package com.xianggu.designMode.decorator.demo02;

import java.io.*;

/**
 * @author chengjy
 * @date 2025/3/19
 */
public class Demo02 {
    public static void main(String[] args) {
        File file = new File("D:\\香菇的大爱\\就好这口\\008tvTN2gy1hkfrnmqub5j326839cx6r.jpg");

        try (FileInputStream inputStream = new FileInputStream(file);
             FileOutputStream outputStream = new FileOutputStream(new File("挺好康.jpg"))) {
            // 记录读取耗时
            long start = System.currentTimeMillis();
            int index = 0;
            long fileSize = file.length();
            byte[] bytes = new byte[8192];
            while (true) {
                int read = inputStream.read(bytes);
                // 每读取1000个字节，打印一次进度
                index += read;
//                if (index++ % 1000 == 0) {
//                    System.out.print("\r读取中！" + index + " / " + fileSize);
//                }
                if (read != -1) {
                    outputStream.write(bytes ,0,read);
                } else {
                    // 读取到文件末尾，跳出循环
                    break;

                }
            }
            System.out.println("\r读取完成！" + index + " / " + fileSize + " 耗时：" + (System.currentTimeMillis() - start) + "ms  读取字节数" + index);


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
